package cn.com.lms.business.examRewrite.persistence.repository;

import cn.com.lms.business.examRewrite.persistence.entity.PaperSubjectReWrite;
import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface PaperSubjectReWriteDao extends BaseRepository<PaperSubjectReWrite,Long> {
    /**
     * 通过id和is_delete获取固定试卷的数据
     * @param id
     * @param is_delete
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT * FROM bnz_exam_paper_subject WHERE paper_id = ?1 AND is_delete = ?2")
    List<PaperSubjectReWrite> findByIdAndIsDelete(Long id, boolean is_delete);


    /**
     * 通过subjectId删除数据
     * @param id
     */
    void deleteById(Long id);

    /**
     * 查询试卷试题列表-固定题
     * @param id
     * @return
     */
    @Query(value = "select ps.paper_id as paperId,ps.score,ps.sort_id as sortId,qs.question_id as  questionId," +
            " qs.type ,qs.id as subjectId,qs.subject,qs.correct_status as correctStatus,qs.is_delete as isDelete ,(select group_concat(CONCAT(option_name,\" \",content) SEPARATOR '###')   from bnz_exam_question_subject_option so LEFT JOIN bnz_exam_question_subject qs  on so.subject_id=qs.id   where so.subject_id=ps.subject_id " +
            " GROUP BY subject_id) options, " +
            " (select group_concat(CONCAT(file_path))   from bnz_exam_question_subject_file sf where sf.subject_id=ps.subject_id " +
            " GROUP BY subject_id) images "+
            " from bnz_exam_paper_subject ps LEFT JOIN bnz_exam_question_subject qs on ps.subject_id=qs.id " +
            " where ps.is_delete=0 and  ps.paper_id in (:id) order by ps.sort_id asc ", nativeQuery = true)
    List<Map<String,Object>> findPaperScoreByPaperIdList(@Param(value = "id") Long id);

    /**
     * 通过PaperId删除
     * @param result
     * @param ids
     */
    @Query(nativeQuery = true,value = "UPDATE bnz_exam_paper_subject SET is_delete = :result WHERE paper_id IN (:ids)")
    @Modifying
    void deletePaperSubjectReWriteByPaperId(@Param("result") boolean result, @Param("ids") List<Long> ids);
}
